<?php

/** class responsiblities:
*	Class is responsible for basic image manipulation
*	from imagedata. File operations should not be handled by this
*	class
*/
class ImageData { //extends JFObject {

	const TOP = "top";
	const CENTER = "center";
	const BOTTOM = "bottom";
	const LEFT = "left";
	const RIGHT = "right";

	protected $imageRes = NULL;

	protected $width;
	protected $height;
	protected $type;

	
	function __construct() {
		
	}
	
	// convenience function | do not allow to save.
	public static function fromFile( $filename ) {
		$imgdata = new self();
		if( file_exists( $filename ) ) $imgdata->imageRes = imagecreatefromstring( file_get_contents($filename) );
		list($width, $height, $type, $attr) = getimagesize($filename);
		$imgdata->findDimensions();
		return $imgdata;
	}
	
	public static function fromString( $string ) {
		$imgdata = new self();
		$imgdata->imageRes = imagecreatefromstring( $string );
		$imgdata->findDimensions();
		return $imgdata;
	}

	private function findDimensions() {
		if( $this->imageRes !== NULL ) {
			$this->width = imagesx($this->imageRes);
			$this->height = imagesy($this->imageRes);
		} else {
			throw new ImageDataException("No image loaded.", ImageDataException::NO_IMAGE_LOADED);
		}
	}
	
	public function crop($width, $height, $ver = self::CENTER, $hor = self::CENTER ) {
		if( $width > $this->width ) throw new JFImageDataException("Cannot crop. New width is larger than old width", ImageDataException::INVALID_DIMENSION);
		if( $heigh > $this->height ) throw new JFImageDataException("Cannot crop. New height is larger than old height", ImageDataException::INVALID_DIMENSION);
		if( $ver === self::TOP ) $y = 0;
		if( $ver === self::CENTER ) $y = floor(($this->height - $height) / 2);
		if( $ver === self::BOTTOM ) $y = $this->height - $height;
		if( $hor === self::LEFT ) $x = 0;
		if( $hor === self::CENTER) $x = floor( ($this->width - $width) / 2 );
		if( $hor === self::RIGHT ) $x = $this->width - $width;
		$newPhoto = imagecreatetruecolor( $width, $height );
		if( !$newPhoto ) throw new Exception('Could not create a new image');
		if( !imagecopyresampled ( $newPhoto, $this->imageRes, 0, 0, $x, $y, $width, $height, $width, $height ) ) throw new Exception('Could not resize image');		
		$this->imageRes = $newPhoto;		
		return $this;
	}

	public function resize($width, $height, $proportional = true) {
		if( $proportional ) {
			$prop = $this->width / $this->height;
			$newProp = $width / $height;
			if( $prop >= $newProp ) {
				$newWidth = $width;
				$newHeight = round( (1/$prop)*$newWidth );
			} else {
				$newHeight = $height;
				$newWidth = round( $prop*$newHeight );
			}
		} else {
			$newWidth = $width;
			$newHeight = $height;
		}

		$newPhoto = imagecreatetruecolor( $newWidth, $newHeight );
		if( !$newPhoto ) throw new Exception('Could not create a new image');
		if( !imagecopyresampled ( $newPhoto, $this->imageRes, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height ) ) throw new Exception('Could not resize image');

		$this->imageRes = $newPhoto;
		$this->width = $newWidth;
		$this->height = $newHeight;
		return $this;
	}
	
	public function get() {
		return $this->imageRes;
	}
	
}